package com.xunyun.kotlinwc.main.adapter


import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.paging.PagedListAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.xunyun.kotlinwc.R
import com.xunyun.kotlinwc.databinding.ItemProjectBinding
import com.xunyun.kotlinwc.main.model.Project

/**
 * @author gl
 * @date 2021/9/10
 * @desc
 */
class ProjectPageAdapter constructor(
    val context: Context,
    diffCallback: DiffUtil.ItemCallback<Project>
) :
    PagedListAdapter<Project, ProjectPageAdapter.ProjectViewHolder>(diffCallback) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProjectViewHolder {

        val binding: ItemProjectBinding = DataBindingUtil.inflate(
            LayoutInflater.from(parent.context),
            R.layout.item_project, parent, false
        )

        return ProjectViewHolder(binding);
    }

    override fun onBindViewHolder(holder: ProjectViewHolder, position: Int) {
        getItem(position)?.run {
            holder.binding.tvTitle.text = title
            holder.binding.tvAuthor.text = getAuthor(this)
            holder.binding.tvDesc.text = desc
            holder.binding.tvTime.text = niceDate
            Glide.with(context)
                .load(envelopePic)
                .placeholder(R.mipmap.login_header_bg)
                .into(holder.binding.ivEnvelope)

        }
    }

    class ProjectViewHolder(var binding: ItemProjectBinding) : RecyclerView.ViewHolder(binding.root)

    private fun getAuthor(project: Project): String {
        if (project.author.isEmpty()) {
            return project.shareUser
        }
        return project.author
    }


}